﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class Sprite2D : VisibleGame
    {
    #region  Properties

        protected Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { 
                _position = value;
                _rectangle.X = (int)_position.X;
                _rectangle.Y = (int)_position.Y;
            }
        }
        public float PositionX
        {
            set { _position.X = value; }
        }

        protected Texture2D _texture;
        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }

        protected Rectangle _rectangle;
        public Rectangle Rectangle
        {
            get { return _rectangle; }
            set { _rectangle = value; }
        }
        protected float _scaleRect;
        public float ScaleRect
        {
            get { return _scaleRect; }
            set { _scaleRect = value; }
        }
    #endregion

    #region Initiation
        public Sprite2D(Texture2D texture,Vector2 posotion)
        {
            _position = posotion;
            _texture = texture;
            if (_texture != null)
            {
                _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _texture.Width, _texture.Height);
            }            
            _scaleRect = 0;
        }
    #endregion

    #region Methods
        protected bool PointInRectangle(Vector2 point, Rectangle rect)
        {
            return (point.Y >= rect.Y - _scaleRect/2 && point.Y <= rect.Height + rect.Y + _scaleRect/2
                && point.X >= rect.X - _scaleRect/2 && point.X <= rect.Width + rect.X + _scaleRect/2);
        }

        protected bool PointInRectangle(Vector2 point, Rectangle rect, float scaleRect)
        {
            return (point.Y >= rect.Y - scaleRect / 2 && point.Y <= rect.Height + rect.Y + scaleRect / 2
                && point.X >= rect.X - scaleRect / 2 && point.X <= rect.Width + rect.X + scaleRect / 2);
        }

        protected bool PointInRealRectangle(Vector2 point, Rectangle rect)
        {
            return (point.Y >= rect.Y  && point.Y <= rect.Height + rect.Y 
                && point.X >= rect.X  && point.X <= rect.Width + rect.X );
        }

        public virtual bool IsCollisionWith(Sprite2D AnotherSprite)
        {
            Rectangle AS_Rect = AnotherSprite.Rectangle;
            return (PointInRectangle(new Vector2(AS_Rect.X, AS_Rect.Y), _rectangle)
                || PointInRectangle(new Vector2((2 * AS_Rect.X + AS_Rect.Width) / 2, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRectangle(new Vector2(AS_Rect.X, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, AS_Rect.Y), _rectangle)
                || PointInRectangle(new Vector2(AS_Rect.X, AS_Rect.Y + AS_Rect.Height), _rectangle)
                || PointInRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, AS_Rect.Y + AS_Rect.Height), _rectangle));
        }

        public bool IsOnlyCollisionWith(Sprite2D AnotherSprite)
        {
            Rectangle AS_Rect = AnotherSprite.Rectangle;
            return (PointInRealRectangle(new Vector2(AS_Rect.X, AS_Rect.Y), _rectangle)
                || PointInRealRectangle(new Vector2((2 * AS_Rect.X + AS_Rect.Width) / 2, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRealRectangle(new Vector2(AS_Rect.X, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRealRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, (2 * AS_Rect.Y + AS_Rect.Height) / 2), _rectangle)
                || PointInRealRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, AS_Rect.Y), _rectangle)
                || PointInRealRectangle(new Vector2(AS_Rect.X, AS_Rect.Y + AS_Rect.Height), _rectangle)
                || PointInRealRectangle(new Vector2(AS_Rect.X + AS_Rect.Width, AS_Rect.Y + AS_Rect.Height), _rectangle));
        }

        public virtual bool IsTouched(Vector2 FingerPosition)
        {
            return PointInRectangle(FingerPosition, _rectangle, -3);
        }
    #endregion

    #region Update & Draw
        public virtual void Update(GameTime gametime,Vector2 position)
        {
            _position = position;
            _rectangle.X = (int)_position.X;
            _rectangle.Y = (int)_position.Y;
        }

        public virtual void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            spritebatch.Draw(_texture, _position, Color.White);
        }
    #endregion
    }
}
